package com.ml.doctor.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.ml.doctor.R;
import com.ml.doctor.bean.UserInfoBean;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.QiNiuUtils;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.UpCompletionHandler;

import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

public class UpdateZhenShuActivity extends BaseActivity {

    public static final int SELECT_ZCZS_CODE = 1;
    public static final int SELECT_YYZZ_CODE = 2;
    @BindView(R.id.iv_zczs)
    ImageView ivZczs;
    @BindView(R.id.iv_yyzz)
    ImageView ivYyzz;
    @BindView(R.id.btn_commit)
    Button btnCommit;
    public String zyImageUrl;
    public String zcImageUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_zhen_shu);
        ButterKnife.bind(this);
        setTopTitle("身份审核");
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    @OnClick({R.id.iv_zczs, R.id.iv_yyzz, R.id.btn_commit})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.iv_zczs:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    checkPermission(SELECT_ZCZS_CODE);
                } else {
                    selectPicture(SELECT_ZCZS_CODE);
                }
                break;
            case R.id.iv_yyzz:
                checkPermission(SELECT_YYZZ_CODE);
                break;
            case R.id.btn_commit:
                NetworkApi.getUserInfo(new NetworkManager.SuccessCallback<UserInfoBean>() {
                    @Override
                    public void onSuccess(UserInfoBean response) {
                       if (response == null){
                           return;
                       }
                        response.documents = zyImageUrl;
                        response.card = zcImageUrl;
                        NetworkApi.updateInfo(response, new NetworkManager.SuccessCallback<UserInfoBean>() {
                            @Override
                            public void onSuccess(UserInfoBean response) {
                                Toast.makeText(mContext, "提交成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void checkPermission(int requestCode){
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
        } else {
            selectPicture(requestCode);
        }
    }

    private void selectPicture(int requestCode){
        Intent intent = new Intent(this, MultiImageSelectorActivity.class);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 0:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                } else {
                    Toast.makeText(mContext, "not permission", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECT_ZCZS_CODE && resultCode == RESULT_OK) {
            final List<String> list = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            Glide.with(this).load(list.get(0)).asBitmap().into(ivZczs);
            NetworkApi.getQiNiuToken(new NetworkManager.SuccessCallback<String>() {
                @Override
                public void onSuccess(String response) {
                    if (TextUtils.isEmpty(response)){
                        return;
                    }
                    QiNiuUtils.getInstance().uploadImage(response, list.get(0), new UpCompletionHandler() {
                        @Override
                        public void complete(String key, ResponseInfo info, JSONObject response) {
                            zyImageUrl = "http://oyptcv2pb.bkt.clouddn.com/" + key;
                        }
                    });
                }
            });
        }
        if (requestCode == SELECT_YYZZ_CODE && resultCode == RESULT_OK) {
            final List<String> list = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            Glide.with(this).load(list.get(0)).asBitmap().into(ivYyzz);
            NetworkApi.getQiNiuToken(new NetworkManager.SuccessCallback<String>() {
                @Override
                public void onSuccess(String response) {
                    if (TextUtils.isEmpty(response)){
                        return;
                    }
                    QiNiuUtils.getInstance().uploadImage(response, list.get(0), new UpCompletionHandler() {
                        @Override
                        public void complete(String key, ResponseInfo info, JSONObject response) {
                            zcImageUrl = "http://oyptcv2pb.bkt.clouddn.com/" + key;
                        }
                    });
                }
            });
        }
    }
}
